package cn.com.aiton.educenter.controller;


import cn.com.aiton.educenter.entity.PestsControl;
import cn.com.aiton.educenter.entity.PestsTrap;
import cn.com.aiton.educenter.entity.UcenterMember;
import cn.com.aiton.educenter.entity.UcenterMemberWorktype;
import cn.com.aiton.educenter.entity.vo.PestsControlQuery;
import cn.com.aiton.educenter.entity.vo.PestsControlVo;
import cn.com.aiton.educenter.entity.vo.UcenterMemberWorktypeQuery;
import cn.com.aiton.educenter.service.UcenterMemberWorktypeService;
import cn.com.qiter.common.Result;
import cn.com.qiter.common.vo.UcenterMemberWorktypeModel;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户的业务类型 前端控制器
 * </p>
 *
 * @author testAuthor
 * @since 2021-05-11
 */
@RestController
@RequestMapping("/educenter/worktype")
public class UcenterMemberWorktypeController {
    @Autowired
    UcenterMemberWorktypeService service;
    //讲师修改功能
    @PostMapping("add")
    public Result add(@RequestBody UcenterMemberWorktypeModel model) {
        UcenterMemberWorktype worktype = new UcenterMemberWorktype();
        BeanUtil.copyProperties(model,worktype);
        boolean flag = service.save(worktype);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @GetMapping("init/{userId}")
    public Result init(@ApiParam(name = "userId", value = "userId", required = true)
                           @PathVariable String userId) {

        boolean flag = service.init(userId);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //1 查询讲师表所有数据
    //rest风格
    @ApiOperation(value = "所有防治列表")
    @GetMapping("findByMemberId/{id}")
    public Result findByMemberId(@ApiParam(name = "id", value = "ID", required = true)
                                     @PathVariable String id) {
        //调用service的方法实现查询所有的操作
        //构建条件
        QueryWrapper<UcenterMemberWorktype> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql

        if(!StringUtils.isEmpty(id)) {
            wrapper.eq("member_id",id);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        List<UcenterMemberWorktype> list = service.list(wrapper);
        return Result.ok().data("rows",list);
    }
    //1 查询讲师表所有数据
    //rest风格
    @ApiOperation(value = "所有防治列表")
    @GetMapping("findAll")
    public Result findAll() {
        //调用service的方法实现查询所有的操作
        List<UcenterMemberWorktype> list = service.list(null);
        return Result.ok().data("items",list);
    }

    //2 逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除防治")
    @DeleteMapping("remove/{memberid}/{type}")
    public Result remove(@ApiParam(name = "type", value = "type", required = true)
                         @PathVariable String type,
                         @ApiParam(name = "memberid", value = "memberid", required = true)
                         @PathVariable String memberid) {
        //调用service的方法实现查询所有的操作
        //构建条件
        QueryWrapper<UcenterMemberWorktype> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql

        if(!StringUtils.isEmpty(type)) {
            wrapper.eq("work_type",type);
        }
        if(!StringUtils.isEmpty(memberid)) {
            wrapper.eq("member_id",memberid);
        }
        boolean flag = service.remove(wrapper);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //2 逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除防治")
    @DeleteMapping("remove/{id}")
    public Result remove(@ApiParam(name = "id", value = "ID", required = true)
                                @PathVariable String id) {
        boolean flag = service.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //3 分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @GetMapping("page/{current}/{limit}")
    public Result page(@PathVariable long current,
                              @PathVariable long limit) {
        //创建page对象
        Page<UcenterMemberWorktype> object = new Page<>(current,limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        service.page(object,null);
        long total = object.getTotal();//总记录数
        List<UcenterMemberWorktype> records = object.getRecords(); //数据list集合

        return Result.ok().data("total",total).data("rows",records);
    }


    //4 条件查询带分页的方法
    @PostMapping("page/{current}/{limit}")
    public Result page(@PathVariable long current, @PathVariable long limit,
                       @RequestBody(required = false) UcenterMemberWorktypeQuery query) {
        //创建page对象
        Page<UcenterMemberWorktype> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<UcenterMemberWorktype> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql

        String begin = query.getBegin();
        String end = query.getEnd();
        String pestsType = query.getWorkType();
        String userId = query.getMemberId();
        //判断条件值是否为空，如果不为空拼接条件

        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        if(!StringUtils.isEmpty(userId)) {
            wrapper.eq("member_id",userId);
        }
        if(!StringUtils.isEmpty(pestsType)) {
            wrapper.eq("work_type",pestsType);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        service.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<UcenterMemberWorktype> records = pageTeacher.getRecords(); //数据list集合

        return Result.ok().data("total",total).data("rows",records);
    }
    @PostMapping("update")
    public Result update(@RequestBody UcenterMemberWorktype eduTeacher) {
        boolean flag = service.updateById(eduTeacher);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}

